import { axiosMock, delayFn, randArrItem } from '@/services/axiosMock'
import { DEFAULT_PAGE_SIZE } from '@/utils/pagination'
import { getLogisticsStatusName } from '../../utils'
import { IOrderListItem, IOrderListRes, IUser } from '../interface'

let count = 0
const img1 = `https://fba-srm.oss-cn-hongkong.aliyuncs.com/1734260133964_test1.png`
const img2 = `https://fba-srm.oss-cn-hongkong.aliyuncs.com/1734260167384_test2.png`

const createUser = (): IUser => {
  const id = ++count
  return {
    id,
    active: randArrItem([true], 0.1) || false,
    username: `账号${id}`,
    roleId: randArrItem([1, 2]),
    name: randArrItem([`用户名称${id}`], 0.1),
    englishName: randArrItem([`用户英文名称${id}`], 0.1),
    avatar: randArrItem([img1, img2], 0.1),
    phone: randArrItem(['18866669999'], 0.1),
    departmentId: randArrItem([id], 0.1),
    position: randArrItem([`职位${id}`], 0.1),
    num: randArrItem([`工号${id}`], 0.1),
    loginDate: Date.now() - 1000 * 60 * 60,
    createdDate: Date.now() - 1000 * 60 * 60 * 24 * 365,
  }
}

axiosMock('default', mock => {
  // 获取运单列表
  mock.onPost(`/tmsapi/waybill_record/list`).reply(
    delayFn<IOrderListRes>(600, ({ data }) => {
      return {
        success: true,
        data: {
          total: 2500,
          records: Array(+data.size || DEFAULT_PAGE_SIZE)
            .fill(null)
            .map((_item, index) => {
              const num = index + 1
              return {
                recordId: num,

                waybillNo: `LH01321208${num}`,

                timeIconType: randArrItem([1, 2]),
                readType: randArrItem([1, 2]),
                errorType: randArrItem([1, 2]),
                emailType: randArrItem([1, 2]),

                // === 日期分组 ===
                age: randArrItem([`12天`, `13天`], 0.1),
                deliveryDate: randArrItem([`2023-11-16 12:12:12`, '2024-11-16 12:12:12'], 0.1),
                etd: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                eta: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                predictSigningTime: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                practicalSigningTime: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),

                // === 渠道分组 ===
                merchantServers: randArrItem([`天图${num}`], 0.1),
                logisticsChannel: randArrItem([`🇺🇸美国芝加哥普船卡派LHUST6${num}`], 0.1),
                channelNo: randArrItem([`LH11231208${num}`], 0.1),
                carrier: randArrItem([`Carrier${num}`], 0.1),
                transferCodes: randArrItem([[`1ZG1962J0324857728${num}1`, `1ZG1962J0328489324${num}2`]], 0.1),
                shipName: randArrItem([`MACKENZIE/4E${num}`], 0.1),
                voyageNumber: randArrItem([`COSCO THAILAND 103E${num}`], 0.1),
                containerNumber: randArrItem([`TGBU4876390${num}`], 0.1),
                flightNumber: randArrItem([`航班号${num}`], 0.1),

                logisticsStatus: randArrItem(getLogisticsStatusName.valueList, 0.1),

                latestRoute: randArrItem([`【起运港】已装载 08-31 ${num}`], 0.1),

                logisticsStatusEn: randArrItem(
                  ['Handling', 'Exporting', 'In Transit', 'Importing', 'Transporting', 'Out For Delivery', 'Delivered'],
                  0.1,
                ),
                latestRouteEn: randArrItem(
                  [
                    '【Port of Loading】LH01410015 has been transported by trailer, and upon arrival at the port, Chavez Theresa waits, F7987987, arrives at ABE1 and prepares for AAA departure',
                    '【Port of Diarge】LH01410015 has been transported by trailer, and upon arrival at the port, Chavez Theresa waits, F7987987',
                    '【Port of Discharge】LH01410015 has been transported by trailer, and upon arrival at the port',
                  ],
                  0.1,
                ),

                // === 地址分组 ===
                destination: randArrItem([`目的地${num}`], 0.1),
                origin: randArrItem([`始发地${num}`], 0.1),
                originPort: randArrItem([`起运港${num}`], 0.1),
                originAirport: randArrItem([`起运机场${num}`], 0.1),
                destinationAirport: randArrItem([`目的机场${num}`], 0.1),
                countryId: randArrItem([1, 2, 3, 4, 5], 0.1),

                customer: randArrItem([`客户${num}`], 0.1),

                staffInfoList: randArrItem(
                  [
                    Array(randArrItem([1, 3, 5]))
                      .fill(null)
                      .map(() => ({
                        staffType: randArrItem([1, 2]),
                        user: createUser(),
                      })),
                  ],
                  0.1,
                ),

                // === 亚马逊分组 ===
                fbaId: randArrItem([`FBA18FZDP0N6${num}`], 0.1),
                reId: randArrItem([`1RJE4EHL${num}`], 0.1),
                predictTimeStart: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                predictTimeEnd: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                practicalDeliveryTimeStart: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                practicalDeliveryTimeEnd: randArrItem([`2023-11-16 12:12:12`, `2024-11-16 12:12:12`], 0.1),
                shipFrom: randArrItem([1, 2], 0.1),

                // === 产品分组 ===
                boxes: randArrItem([
                  Array(randArrItem([1, 2, 3]))
                    .fill(null)
                    .map(
                      (_item, index) =>
                        ({
                          boxNum: randArrItem([1, 2, 3]),
                          products: Array(randArrItem([1, 2, 3]))
                            .fill(null)
                            .map(
                              (_, i) =>
                                ({
                                  boxId: +`${index + 1}${i + 1}`,
                                  cname: randArrItem([`中文品名${i}`], 0.1),
                                  ename: randArrItem([`英文品名${i}`], 0.1),
                                  sku: randArrItem([`SKU${i}`], 0.1),
                                  fnsku: randArrItem([`FNSKU${i}`], 0.1),
                                }) satisfies IOrderListItem['boxes'][number]['products'][number],
                            ),
                        }) satisfies IOrderListItem['boxes'][number],
                    ),
                ]),
                createShippingOrderStatus: randArrItem([0, 1], 0.1),
                $boxNum: randArrItem([10, 20, 30], 0.1), // 由 boxes[n].boxNum 累计
              } satisfies IOrderListItem
            }),
        },
      }
    }),
  )
})
